/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2003-2008 University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.gui2.sort;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JToolTip;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.JTableHeader;

import edu.umd.cs.findbugs.gui2.BugTreeModel;
import edu.umd.cs.findbugs.gui2.Debug;
import edu.umd.cs.findbugs.gui2.Driver;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class SorterTableHeader extends JTableHeader {

	private SorterTableColumnModel sorter;
	
	public SorterTableHeader(SorterTableColumnModel sorter) {
		this.sorter = sorter;
		initialise();
	}

	private void initialise() {
		setColumnModel(sorter);
		setToolTipText(edu.umd.cs.findbugs.L10N.getLocalString("tooltip.reorder_message",
				"Drag to reorder tree folder and sort order"));
		
		// Sets the size of the tooltip to match the rest of the GUI. - Kristin
		JToolTip tempToolTip = createToolTip();
		UIManager.put("ToolTip.font", new FontUIResource(tempToolTip.getFont().deriveFont(Driver.getFontSize())));
	}
	
	private final Map<SortableBugTree, SorterTableMouseListener> treeToListenerMap = 
		new HashMap<SortableBugTree, SorterTableMouseListener>();
	
	public void addSorterMouseListener(SortableBugTree tree) {
		SorterTableMouseListener newListener = new SorterTableMouseListener(this, tree);
		treeToListenerMap.put(tree, newListener);
		addMouseListener(newListener);
	}
	
	public void removeSorterMouseListener(SortableBugTree oldTree) {
		SorterTableMouseListener toRemove = treeToListenerMap.remove(oldTree);
		removeMouseListener(toRemove);
		
	}
	

	private static final class SorterTableMouseListener extends MouseAdapter {
		private SorterTableHeader tableHeader;
		private final SortableBugTree tree;
		
		public SorterTableMouseListener(SorterTableHeader tableHeader, SortableBugTree tree) {
			this.tableHeader = tableHeader;
			this.tree = tree;
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			Debug.println("tableheader.getReorderingAllowed() = " + tableHeader.getReorderingAllowed());
			if (!tableHeader.getReorderingAllowed())
				return;
			if (e.getClickCount() == 2)
				SorterDialog.getInstance().setVisible(true);
		}
		
		@Override
		public void mouseReleased(MouseEvent arg0) {
			if (!tableHeader.getReorderingAllowed())
				return;
			BugTreeModel bt = (BugTreeModel) (tree.getModel());
			bt.checkSorter();
		}

		@Override // Generated
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			SorterTableMouseListener other = (SorterTableMouseListener) obj;
			if (tableHeader == null) {
				if (other.tableHeader != null)
					return false;
			} else if (!tableHeader.equals(other.tableHeader))
				return false;
			if (tree == null) {
				if (other.tree != null)
					return false;
			} else if (!tree.equals(other.tree))
				return false;
			return true;
		}
		
	}
}

